
import sys
from datetime import timedelta
from datetime import datetime

monthIDs = {"Jan":1,
            "Feb":2,
            "Mar":3,
            "Apr":4,
            "May":5,
            "Jun":6,
            "Jul":7,
            "Aug":8,
            "Sep":9,
            "Oct":10,
            "Nov":11,
            "Dec":12}

def getSensorID_AB(oldID):

    # bitmap
    # 10 : sensor of house A
    # 01 : sensor of house B
    # 00 : sensor of house C
    # 11 : shared sensor

    """ These are sensor correspondences between house A and B """
    """
    if oldID in ['A6','B1']: return "111"
    if oldID in ['A23','B5']: return "211"
    if oldID in ['A8','B3']: return "311"
    if oldID in ['A14','B6']: return "411"
    if oldID in ['A12','B7']: return "511"
    if oldID in ['A24','B10']: return "611"
    if oldID in ['A1','B27']: return "711"
    if oldID in ['A9','B9']: return "811"
    """

    """ Sensors are not shared among the two houses """
    if oldID[0] is 'A': return "%s10" % oldID[1:]
    if oldID[0] is 'B': return "%s01" % oldID[1:]
    if oldID[0] is 'C': return "%s00" % oldID[1:]

    assert False

def writeCorrespondences(outFile):
    outFile.write("sensor correspondences:\n")
    outFile.write("111: A6=B1 (bathroom door)\n")
    outFile.write("211: A23=B5 (cupboard groceries)\n")
    outFile.write("311: A8=B3 (fridge)\n")
    outFile.write("411: A14=B6 (toilet flush)\n")
    outFile.write("511: A12=B7 (front door)\n")
    outFile.write("611: A24=B10 (bedroom door)\n")
    outFile.write("711: A1=B27 (microwave)\n")
    outFile.write("811: A9=B9 (cupboard plates)\n\n")

def weekendDay (dt):
    return dt.date().weekday() in [5, 6]

def readSensorFirings(filename, houseLabel, firing):
    f = open(filename,"r")
    for line in f:
        line = line.strip().split('\t')

        start = line[0].split()
        dstart = start[0].split('-')
        dstart[1] = monthIDs[dstart[1]]
        dstart = [int(x) for x in dstart]
        tstart = start[1].split(':')
        tstart = [int(x) for x in tstart]

        stop = line[1].split()
        dstop = stop[0].split('-')
        dstop[1] = monthIDs[dstop[1]]
        dstop = [int(x) for x in dstop]
        tstop = stop[1].split(':')
        tstop = [int(x) for x in tstop]

        dtStart = datetime(dstart[2], dstart[1], dstart[0], tstart[0], tstart[1], tstart[2])
        dtStop = datetime(dstop[2], dstop[1], dstop[0], tstop[0], tstop[1], tstop[2])

        startPoint = tstart[0]*60 + tstart[1] + (0 if tstart[2] < 30 else 1)

        duration = str(dtStop - dtStart)
        if "days" in duration:
            duration = duration.split(' days, ')
            days = int(duration[0])
            duration = duration[1].split(':')
            duration = [int(x) for x in duration]
            duration[0] += (days * 24)
        elif "day" in duration:
            duration = duration.split(' day, ')
            days = int(duration[0])
            duration = duration[1].split(':')
            duration = [int(x) for x in duration]
            duration[0] += (days * 24)
        else:
            duration = [int(x) for x in str(dtStop - dtStart).split(':')]

        # minutes
        #duration = duration[0]*60 + duration[1] + (0 if duration[2] < 30 else 1)

        # seconds
        duration = duration[0]*60*60 + duration[1]*60 + duration[2]

        isWeekendDay = 1 if weekendDay(dtStart) else 0

        sensorID = getSensorID_AB(houseLabel + line[2])
        #sensorID = line[2]

        firings.append([sensorID, startPoint, duration, isWeekendDay])

firings = []

#readSensorFirings("../lena/houseInfoA/houseA-ss.txt", "A", firings)
readSensorFirings("../lena/houseInfoB/houseB-ss.txt", "B", firings)
readSensorFirings("../lena/houseInfoC/houseC-ss.txt", "C", firings)

out = open("firings_BC_notmerged_sec.txt","w")

"""
out.write("\n")
writeCorrespondences(out)
out.write("fields:\n"
          "f1: sensorID\n"
          "    *10: house A\n"
          "    *01: house B\n"
          "    *11: shared\n"
          "f2: startPoint (minute of day)\n"
          "f3: duration (in minutes)\n"
          "f4: weekend (1=weekend, 0=weekday)\n\n")
"""
for x in firings:
    out.write(str(x[0]) + ";")
    out.write(str(x[1]) + ";")
    out.write(str(x[2]) + ";")
    out.write(str(x[3]) + "\n")

out.close()

